package com.nuoniu.sibanyun.controller.production;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.VProductionOrderMainSaveDto;
import com.nuoniu.sibanyun.entity.production.VProductionOrderMain;
import com.nuoniu.sibanyun.service.production.IVProductionOrderMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 生产订单
 * @auther 小啊刚
 * @date 2021-03-09
 */
@RestController
@Api(value = "生产订单管理", tags = "生产订单管理")
@RequestMapping("/production/order")
public class VProductionOrderController {

    @Autowired
    private IVProductionOrderMainService productionOrderMainService;

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VProductionOrderMain productionOrderMain) {
        if(productionOrderMain == null) {
            throw new NuoNiuException("数据不能为空");
        }
        if(productionOrderMain.getOrderDetailList() == null || productionOrderMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("单据明细不能为空");
        }

        productionOrderMainService.saveOrder(productionOrderMain);
        return HttpResult.ok();
    }

    @ApiOperation("根据客户 分单")
    @PostMapping("saveOrderForCustomer")
    public HttpResult saveOrderForCustomer(@RequestBody VProductionOrderMainSaveDto productionOrderMainSaveDto) {
        if(productionOrderMainSaveDto == null) {
            throw new NuoNiuException("数据不能为空");
        }
        if(productionOrderMainSaveDto.getOrderDetailList() == null || productionOrderMainSaveDto.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("单据明细不能为空");
        }
        productionOrderMainService.saveOrderForCustomer(productionOrderMainSaveDto);
        return HttpResult.ok();
    }


    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(productionOrderMainService.getCode());
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        productionOrderMainService.delete(id);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        productionOrderMainService.submit(id, status);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
       productionOrderMainService.examine(id, status);
       return HttpResult.ok();
    }


}
